﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MedievalGameProject
{
    public class Character : Actor
    {
        public Texture2D healthBarTexture;

        public Vector3 spawnLocation;
        public bool isLocal;
        public float maxHealth;
        public float currentHealth;
        public Controller controller;
        public Weapon activeWeapon;

        // TODO: add isLocal boolean property to Character class & include in constructor

        public Character(Game game, BasicModel m, Vector3 position, float actorHeight)
            : base(game, m, position, actorHeight)
        {
            maxHealth = 4;
            currentHealth = maxHealth;
            spawnLocation = position;

            healthBarTexture = Game.Content.Load<Texture2D>("Sprites/healthbar");
        }

        public override void Update(GameTime gameTime)
        {
            if(controller != null)
                controller.Update(gameTime);

            if (Game1.IsDebug())
                Console.WriteLine(position);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            activeWeapon.Draw(gameTime);
        }

        public void DrawHealthBar(SpriteBatch spriteBatch, GameTime gameTime)
        {
            int startDrawXPos = Game.Window.ClientBounds.Width/2 - (healthBarTexture.Width/2) * (int) currentHealth;
            int startDrawYPos = Game.Window.ClientBounds.Height - 40;

            for (int i = 0; i < currentHealth; i++)
            {
                spriteBatch.Draw(healthBarTexture, new Rectangle(startDrawXPos, startDrawYPos, healthBarTexture.Width, healthBarTexture.Height), Color.White);
                startDrawXPos += healthBarTexture.Width;
            }
        }

        public void FireWeapon()
        {
            if (activeWeapon != null)
            {
                Game1.soundManager.PlayCue(SoundManager.SHOOT);
                activeWeapon.BeginFire();
            }
        }

        public void FireWeapon(Vector3 position, Vector3 direction, float speed)
        {
            if (activeWeapon != null)
            {
                Game1.soundManager.PlayCue(SoundManager.SHOOT_2);
                activeWeapon.BeginFire(position, direction, speed);
            }
        }

        public void TakeDamage(float damage, Character damageOwner)
        {
            currentHealth -= damage;
            Console.WriteLine(damage + " damage taken!");

            Game1.soundManager.PlayCue(SoundManager.GOT_HIT);

            if (currentHealth < 1)
                HandleDeath(damageOwner);
        }

        public void HandleDeath(Character damageOwner)
        {
            if (isLocal)
            {
                Game1.instance.SendStartNewRoundMessage();
                Game1.soundManager.PlayCue(SoundManager.YOU_LOSE_2);
            }
        }

        public void Respawn()
        {
            position = spawnLocation;
            currentHealth = maxHealth;
        }

        public void SetController(Controller controller)
        {
            this.controller = controller;
            controller.CurrentActor = this;
        }
    }
}
